package action.adapter;

import java.util.ArrayList;

import action.newelifile.CopyFTPPopup;
import action.newelifile.R;
import action.pojo.FtpConnect;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CopyFTPPopupAdapter extends BaseAdapter{

	Context context;
	ArrayList<FtpConnect> arrayList;
	ViewHolder holder;
	int layoutid;

	public CopyFTPPopupAdapter(CopyFTPPopup context,int textViewResourceId,ArrayList<FtpConnect> arrayList){
		this.context=context;
		this.arrayList=arrayList;
		this.layoutid=textViewResourceId;

	}
	@Override
	public int getCount() {
		return arrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrayList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final FtpConnect ftpConnect = arrayList.get(position);
		if(convertView==null){
			holder = new ViewHolder();
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=layoutInflater.inflate(layoutid, parent,false);
			holder.txtname = (TextView) convertView.findViewById(R.id.name);
			holder.txtinfo = (TextView)convertView.findViewById(R.id.info);
			holder.imiIcon = (ImageView) convertView.findViewById(R.id.icon);
			holder.imiCheck =(ImageView)convertView.findViewById(R.id.checked);
			holder.imiFavorite = (ImageView)convertView.findViewById(R.id.favorite);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		if(ftpConnect!=null){
			if(ftpConnect.getFtpFile()!=null){
				if(ftpConnect.getFtpFile().getName()!=null && !ftpConnect.getFtpFile().getName().equals("")){
					
					holder.txtname.setText(ftpConnect.getFtpFile().getName());
					
					holder.imiIcon.setImageDrawable(ftpConnect.getIcon());
					
					if(ftpConnect.isFavorite()){
						holder.imiFavorite.setVisibility(ImageView.VISIBLE);	
					}else{
						holder.imiFavorite.setVisibility(ImageView.GONE);
					}
					
					if(ftpConnect.isChecked()){
						holder.imiCheck.setVisibility(ImageView.VISIBLE);
					}else{
						holder.imiCheck.setVisibility(ImageView.GONE);
					}
					
					if(holder.txtinfo != null){
						holder.txtinfo.setText(ftpConnect.getInfodetail(context.getString(R.string.folder),context.getString(R.string.file)));
					}
				}

			}
		}

		return convertView;
	}

	public static class ViewHolder {
		TextView txtname,txtinfo;
		ImageView imiIcon,imiCheck,imiFavorite;
	}
}
